/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.PetStoreDao;
import entity.Pet;
import entity.PetOwner;
import entity.PetStore;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import until.BaseDAO;

/**
 *
 * @author Administrator
 */
public class PetStoreMYSQLImpl extends BaseDAO implements PetStoreDao {

    @Override
    public List<PetStore> getAllStoreInfo() {
        List<PetStore> ls = new ArrayList<>();
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from petstore");

            while (rs.next()) {
                PetStore ps = new PetStore();
                ps.setId(rs.getInt("ID"));
                ps.setName(rs.getString("NAME"));
                ps.setPassword(rs.getString("PASSWORD"));
                ps.setBalance(rs.getInt("BALANCE"));
                ls.add(ps);
            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ls;
    }

    @Override
    public PetStore slectStore(String name, String password) {
        PetStore petstore = null;
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from petstore where NAME='" + name + "' and PASSWORD='" + password + "'");

            while (rs.next()) {
                petstore = new PetStore();
                petstore.setId(rs.getInt("ID"));
                petstore.setBalance(rs.getInt("BALANCE"));

            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return petstore;
    }

    @Override
    public int update(Pet pet, PetOwner po) {
        int state = 0;
        try {
            Connection con = BaseDAO.getConnection();
            con.createStatement().execute("update petstore set BALANCE=(BALANCE+" + pet.getPrice() + ") where ID=" + pet.getStore_id());
            state = 1;
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return state;
    }

    @Override
    public int updateSell(Pet pet, int petStoreId) {
        int state = 0;
        try {
            Connection con;
            con = BaseDAO.getConnection();
            con.createStatement().execute("update petstore set BALANCE=(BALANCE-" + pet.getPrice() + ") where ID=" + petStoreId);
            state = 1;
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return state;
    }

}
